package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.CheckinAssessClassDTO;
import com.api.shopapi.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

/**
 * 评估分类
 */
@RestController
@RequestMapping("CheckinAssessClass")
public class CheckinAssessClassController extends BaseController {

    @Autowired
    @Qualifier("CheckinAssessClassServiceImplOne")
    private CheckinAssessClassService checkinAssessClassService;

    @Autowired
    @Qualifier("CheckinAssessItemServiceImplOne")
    private CheckinAssessItemService checkinAssessItemService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    @Autowired
    @Qualifier("CheckinAssessGroupServiceImplOne")
    private CheckinAssessGroupService checkinAssessGroupService;

    @Autowired
    @Qualifier("InstitutionStaffRelServiceImplOne")
    private InstitutionStaffRelService institutionStaffRelService;

    @Autowired
    @Qualifier("UserServiceImplOne")
    private UserService userService;



    @RequestMapping(value = "Add", method = RequestMethod.POST)
    public MyResult Add(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            String name = jsonObject.getString("name");
            int calculationType = jsonObject.getInteger("calculationType");
            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/CheckinAssessClass/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            CheckinAssessClass checkinAssessClass = checkinAssessClassService.getByNameAndInstitutionCode(name, institutionCode);
            if (checkinAssessClass != null) {
                return failed("此评估分类已存在");
            }
            checkinAssessClass = new CheckinAssessClass();
            String checkinAssessClassCode = UUID.randomUUID().toString();
            checkinAssessClass.setCheckinAssessClassCode(checkinAssessClassCode);
            checkinAssessClass.setName(name);
            checkinAssessClass.setStatus(1);
            checkinAssessClass.setCalculationType(calculationType);
            checkinAssessClass.setStaffCode(staffCode);
            checkinAssessClass.setInstitutionCode(institutionCode);

            checkinAssessClassService.insert(checkinAssessClass);
            Object data = new Object[]{};
            return success(data);
        } catch (Exception e) {
            insertError("/CheckinAssessClass/Add", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    @RequestMapping(value = "Edit", method = RequestMethod.POST)
    public MyResult Edit(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            String checkinAssessClassCode = jsonObject.getString("checkinAssessClassCode");
            String name = jsonObject.getString("name");
            int status = jsonObject.getInteger("status");
            int calculationType = jsonObject.getInteger("calculationType");
            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/CheckinAssessClass/Edit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            CheckinAssessClass checkinAssessClass = checkinAssessClassService.getByNameAndInstitutionCode(name, institutionCode);
            if (checkinAssessClass != null) {
                if (!checkinAssessClass.getCheckinAssessClassCode().equals(checkinAssessClassCode)) {
                    return failed("此评估分类已存在");
                }
            } else {
                checkinAssessClass = checkinAssessClassService.getByCheckinAssessClassCode(checkinAssessClassCode);
                if (checkinAssessClass == null) {
                    return failed("数据不存在");
                }
            }

            checkinAssessClass.setName(name);
            checkinAssessClass.setStatus(status);
            checkinAssessClass.setCalculationType(calculationType);
            checkinAssessClassService.updateByCheckinAssessClassCode(checkinAssessClass);

            Object data = new Object[]{};
            return success(data);
        } catch (Exception e) {
            insertError("/CheckinAssessClass/Edit", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    @RequestMapping(value = "Page", method = RequestMethod.POST)
    public MyResult Page(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int status = jsonObject.getInteger("status");
            int pageSize = jsonObject.getInteger("pageSize");
            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/CheckinAssessClass/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            PageInfo<CheckinAssessClass> pageInfocheckinAssessClass = checkinAssessClassService.getPageList(pageIndex, pageSize, institutionCode, status);
            List<CheckinAssessClassDTO> checkinAssessClassDTOList = new ArrayList<>();
            for (CheckinAssessClass checkinAssessClass:pageInfocheckinAssessClass.getList()){
                CheckinAssessClassDTO checkinAssessClassDTO = mapper.map(checkinAssessClass,CheckinAssessClassDTO.class);
                checkinAssessClassDTOList.add(checkinAssessClassDTO);
            }
            for (CheckinAssessClassDTO checkinAssessClassDTO:checkinAssessClassDTOList){
                //统计总分数和总项数
                List<CheckinAssessGroup> checkinAssessGroupList = checkinAssessGroupService.getByCheckinAssessClassCode(checkinAssessClassDTO.getCheckinAssessClassCode());
                int sumScore = 0;
                if(checkinAssessGroupList.size()==0 || checkinAssessGroupList==null){
                    checkinAssessClassDTO.setSumGroup(0);
                    checkinAssessClassDTO.setSumScore(0);
                }else {
                    for(CheckinAssessGroup checkinAssessGroup:checkinAssessGroupList){
                        int maxFraction = checkinAssessGroup.getMaxFraction();
                        sumScore =sumScore+maxFraction;
                    }
                    checkinAssessClassDTO.setSumGroup(checkinAssessGroupList.size());
                    checkinAssessClassDTO.setSumScore(sumScore);
                }
            }
            pageInfocheckinAssessClass.setList(Collections.unmodifiableList(checkinAssessClassDTOList));

            return success(pageInfocheckinAssessClass);
        } catch (Exception e) {
            insertError("/CheckinAssessClass/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //ins
    @RequestMapping(value = "List", method = RequestMethod.POST)
    public MyResult List(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/CheckinAssessClass/List");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            List<CheckinAssessClass> checkinAssessClassDTOList = checkinAssessClassService.getList(institutionCode);
            return success(checkinAssessClassDTOList);
        } catch (Exception e) {
            insertError("/CheckinAssessClass/List", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //ins app
    @RequestMapping(value = "InsListApp", method = RequestMethod.POST)
    public MyResult InsListApp(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/CheckinAssessClass/InsListApp");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            User user = userService.getByUserCode(userCode);
            String institutionCode = user.getInstitutionCode();

            List<CheckinAssessClass> list = checkinAssessClassService.getList(institutionCode);

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("list",list);
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/CheckinAssessClass/InsListApp", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }


}
